package com.termine.jsf;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.event.ScheduleEntrySelectEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleModel;

import com.termine.bean.Bean;
import com.termine.entity.ClientProviderScheduleEvent;
import com.termine.util.Logger;

@ManagedBean
@SessionScoped
public class ClientScheduleJSF extends Bean implements Serializable {

	private static final long serialVersionUID = 7747944240315732664L;

	private ScheduleModel clientScheduleModel;
	private ClientProviderScheduleEvent event;

	/*
	 * Init
	 */
	@PostConstruct
	public void init() {
		try {
			this.event = new ClientProviderScheduleEvent();
			clientScheduleModel = new DefaultScheduleModel();
			List<ClientProviderScheduleEvent> appointments = getAppointmentDAO()
					.getAllAppointmentsForClient(
							getLoginJSF().getPersonalData().getClient_id());
			for (int i = 0; i < appointments.size(); i++) {
				clientScheduleModel.addEvent(appointments.get(i));
			}
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
		}
	}

	/*
	 * Jsf Action Methods
	 */
	public String cancelAppointment() {
		if (getAppointmentManager().cancelAppointment(event)) {
			init();
			return "SUCCESS";
		}
		return "ERROR";
	}
	
	public void onEventSelect(ScheduleEntrySelectEvent selectEvent) {
		event = (ClientProviderScheduleEvent) selectEvent.getScheduleEvent();
	}

	/*
	 * Getters & Setters
	 */
	public ScheduleModel getClientScheduleModel() {
		return clientScheduleModel;
	}

	public void setClientScheduleModel(ScheduleModel clientScheduleModel) {
		this.clientScheduleModel = clientScheduleModel;
	}

	public ClientProviderScheduleEvent getEvent() {
		return event;
	}

	public void setEvent(ClientProviderScheduleEvent event) {
		this.event = event;
	}

}
